package com.seari.vehbaseF.client;

import com.seari.vehbaseF.main.StartUpClass;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

public class RealDataHttpStarter implements Runnable{

    protected Logger logger = LoggerFactory.getLogger(RealDataHttpStarter.class);
    private String[] dataCodeArray = null;
    private static ExecutorService executorService = Executors.newCachedThreadPool();
    private String url = "";
    private int interval ;

    @Override
    public void run() {
        init();

        for (int i = 0; i < dataCodeArray.length; i++) {
            executorService.execute(new RealDataHttpFetcher(dataCodeArray[i],url,interval,this));
        }
    }

    private void init() {
        String realDataHTTPCode = StartUpClass.config.getProperty("RealDataHTTP_code");
        dataCodeArray = StringUtils.split(realDataHTTPCode,"|");
        url = StartUpClass.config.getProperty("RealDataHTTP_url");
        interval = Integer.parseInt(StartUpClass.config.getProperty("RealDataHTTP_interval"));
    }

    public void resetFetcher(RealDataHttpFetcher realDataHttpFetcher) {
        logger.info("reset worker");
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.execute(realDataHttpFetcher);
    }


}
